	PROGRAM GATHER
C	
C	FIND AND PUT ON TEMP. FILE ALL REQUIRED DATA FOR THIS RUN
C
!        1. CODE 17-UNREGULATED STREAM FLOW PUT IN, DATA IS HANDLED AS
!           A GAGE.   10/26/84
!	 2. ALLOW RECORDED STREAM FLOW (CODE 6) TO BE USED IN PLACE OF
!	    RESERVOIR OUTFLOW (CODE 3).  1/10/85
!        3. LOGIC CHANGE TO PUT AVERAGE ANTECEDENT RUNOFF IN AS CODE 17
!           (UNREGULATED FLOW) WHEN AVERAGE IS REQUIRED.  3/13/85
C	 4. LOGIC CHANGED TO HANDLE ACTUAL PCODES NOT NUMBERS AND TO
C	    HANDLE NEW HYDROMET FORMAT. 11/86
!
!      SUBSCRIPT OF PNAME CHANGED FROM 13 TO 17. ie: #1 ABOVE    10/26/84
 	DIMENSION PNAME(17),NAMEST(3),IEQUAT(3)
	DIMENSION DATAr(12),KNAME(2),KTYPE(2),dataa(12)
	CHARACTER*10 NAMEST,IEQUAT,AD*25,ADA*25
	CHARACTER*20 PNAME
	CHARACTER*9  DATAA
	CHARACTER*4  KNAME*12,IYR,KYR,LYR
	CHARACTER*9  KTYPE,NB2
C	
C	IST=SITE FILE INPUT,IND=MAIN DATA INPUT FILE
C       IND=INPUT FILE OF MONTHLY DATA
C	IDTA=OUTPUT FILE WITH DATA REQUIRED FOR THIS JOB
C	IOER=ERROR FILE
C
	DATA IST,IND,IDTA,IOER/5,7,12,16/
!       ITEMS OF CONSTANT DATA ADDED 14 THROUGH 17. ie: #1 ABOVE  10/26/84
	DATA PNAME/'END OF MONTH CONTENT','NOT USED            ',
	1          'RESERVOIR OUTFLOW   ','NOT USED            ',
	2          'CANAL FLOW          ','RECORDED STREAM FLOW',
	3          'PRECIPITATION       ','AVE.PRECIPITATION   ',
	4          'SNOW WATER CONTENT  ','AVE.SNOW WATER ACCUM',
	5          'SOIL MOISTURE       ','AVE.SNOW MOIST ACCUM',
	6          'RESERVOIR INFLOW    ','NOT USED            ',
	7          'NOT USED            ','NOT USED            ',
	8          'UNREG. STREAM FLOW  '/
	IERR = 0
	MMF = 0
	JERR = 0
	WRITE(IOER,5)
5	FORMAT('0', ' ERROR FOR GATHER PROG. EXECUTION' / )
        OPEN(UNIT=12,STATUS='NEW',CARRIAGECONTROL='LIST')
	OPEN(UNIT=7,FILE='dmsdisk:[MPOLL]MPOLL.IND',
	1STATUS='OLD',ORGANIZATION='INDEXED',ACCESS='KEYED',
	2RECORDTYPE='FIXED',SHARED,FORM='UNFORMATTED',RECL=22,
	3KEY=(1:25:CHARACTER),IOSTAT=IOS,ERR=299,READONLY)
	GO TO 300
299	WRITE(IOER,6) IOS
6	FORMAT('0', 'ERROR ON OPENING OF UNIT 7,IOS= ',I2)
	GO TO 600
C
C	READ CORRECT WATER YEAR FROM SITE FILE
300	READ(IST,7) IYR
7	FORMAT(A4)
	DECODE(4,8,IYR) ITMP
8	FORMAT(I4)
	ITMP = ITMP - 1
	ENCODE(4,8,KYR) ITMP
C
C	READ SITE FILE DATA
305	READ(IST,10,END=550) ITYPS,(NAMEST(I),I=1,3),(IEQUAT(I),
	1I=1,3),NMF
10	FORMAT(I2,6A10,I2)
	READ(IST,12) (KNAME(I),KTYPE(I), I=1,2)
12	FORMAT(2(A12,A9))
	AD = ' '
310	IF(MMF .EQ. NMF) GO TO 315
	MMF = NMF
315	IF(KTYPE(1) .EQ. 'QC       ') THEN
C
C	CANAL DATA (CANAL FLOW)
	   NA = ITYPS
	   NB = NA
	   NA1 = 5
	   NB1 = 5
	   NB2 = 'QC       '
	   LYR = KYR
	ELSE IF(KTYPE(1) .EQ. 'QM       ') THEN
C	GAGE DATA
	   NA=ITYPS
	   NB=NA
	   NA1=6
	   NB1=6
	   NB2='QM       '
	   LYR=KYR
	ELSE IF(KTYPE(1) .EQ. 'PM       ') THEN
C	PRECIPITATION DATA
	   NA=ITYPS
	   NB=NA + 20
	   NA1=7
	   NB1=8
	   NB2='PMA      '
	   LYR='9999'
	ELSE IF(KTYPE(1) .EQ. 'AF       ') THEN
C	EOM CONTENT DATA
	   NA=7
	   NB=7
	   NA1=1
	   NB1=1
	   NB2='AF       '
	   LYR=KYR
	ELSE IF(KTYPE(1) .EQ. 'SU       ') THEN
C	SNOW WATER CONTENT DATA
	   NA=ITYPS
	   NB=NA + 20
	   NA1=9
	   NB1=10
	   NB2='SUA      '
	   LYR='9999'
	ELSE IF(KTYPE(1) .EQ. 'SE       ') THEN
C	SNOW WATER CONTENT DATA
	   NA=ITYPS
	   NB=NA + 20
	   NA1=9
	   NB1=10
	   NB2='SEA      '
	   LYR='9999'
	ELSE IF(KTYPE(1) .EQ. 'SM       ') THEN
C	SOIL MOISTURE DATA
	   NA=ITYPS
	   NB=NA + 20
	   NA1=11
	   NB1=12
	   NB2='SMA      '
	   LYR='9999'
!	NEXT 8 STATEMENTS INSERTED 10/26/84 #1
	ELSE IF(KTYPE(1) .EQ. 'QU       ') THEN
C	UNREGULATED STREAM FLOW
	   NA=ITYPS
	   NB=NA
	   NA1=17
	   NB1=17
	   NB2='QU       '
	   LYR=KYR
!       FOLLOWING 6 STATEMENTS ADDED.  #3 ABOVE
	   IF(ITYPS .EQ. 9) THEN
	     LYR='9999'
	     NB=10
	     NC=2
	     GO TO 325
	   END IF
	END IF
C
C	INPUT REQUIRED DATA
C
	NC=1
325	IF(NC .EQ. 1) THEN
	   AD=KNAME(1)//KTYPE(1)//IYR
	ELSE IF(NC .EQ. 2) THEN
	   AD=KNAME(1)//NB2//LYR
	   NA=NB
	   NA1=NB1
	END IF
	READ(IND,KEY=AD(1:25),KEYID=0,IOSTAT=IOS,ERR=375)ADA,DATAR
	ENCODE(108,14,DATAA,iostat=ios,err=385) DATAR
14	FORMAT(12F9.2)
	DO 330 I=1,12
	IF(DATAA(I)(1:6) .NE. '998877' ) GO TO 330
	DATAA(I) = '         '
330	CONTINUE
C
C	WRITE NEW MAIN DATA FILE
	WRITE(IDTA,20) NMF,NAMEST,NA,ADA(22:25),(DATAA(I)(3:9),I=1,12)
20	FORMAT(I2,3A10,I2,A4,12a7)
C
C	INPUT PREVIOUS YEARS DATA OR AVERAGE DATA
	NC=NC + 1
	IF(NC .EQ. 2) GO TO 325
	IF(KTYPE(1) .NE. 'AF       ') GO TO 305
	NC=1
C
C	OUTFLOW DATA REQUIRED
	NA=8
	NB=8
!	NEXT 5 STATEMENTS AND "END IF" INSERTED 1/10/85 #2
	IF(KTYPE(2) .EQ. 'QM       ') THEN
	  NA1=6
	  NB1=6
	  NB2='QM       '
	  ELSE
	  NA1=3
	  NB1=3
	  NB2='OM       '
	END IF
	LYR=KYR
	KNAME(1)=KNAME(2)
	KTYPE(1)=KTYPE(2)
	GO TO 325
C
C	ERROR ON READING MAIN DATA FILE
375	WRITE(IOER,30) IOS, PNAME(NA1),NAMEST,IEQUAT,AD
30	FORMAT('0', 'IOS=',I2,' ERROR IN READING ',A20/
	1' FOR SITE ',3A10,' IN EQUATION ',3A10/
     2'PARAMETER CODE ',A)
	IERR=1
	GO TO 305
c
c       error on encode of main data
  385   Write(ioer,35) ios,pname(na1),namest,iequat,AD
35      format('0','ios=',i2,'error in encode of main data ',
     1A20/' for site ',3a10,' in equation ',3a10/
     2'PARAMETER CODE ',A)
        go to 550
C
C	CLOSE FILE AND END RUN
550	CLOSE(UNIT=7)
        CLOSE(UNIT=12)
600	STOP 1000
	END
